<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>深入理解 CMS 垃圾收集器中的浮动垃圾</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
        }
        .serif-font {
            font-family: 'Noto Serif SC', serif;
        }
        .gradient-text {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            background-clip: text;
        }
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        .card-hover {
            transition: all 0.3s ease;
        }
        .card-hover:hover {
            transform: translateY(-4px);
            box-shadow: 0 20px 40px rgba(0,0,0,0.1);
        }
        .floating-animation {
            animation: float 6s ease-in-out infinite;
        }
        @keyframes float {
            0% { transform: translateY(0px); }
            50% { transform: translateY(-20px); }
            100% { transform: translateY(0px); }
        }
        .drop-cap {
            float: left;
            font-size: 4rem;
            line-height: 3rem;
            padding-right: 0.5rem;
            margin-top: 0.5rem;
            font-weight: 700;
            color: #667eea;
        }
        .section-divider {
            position: relative;
            text-align: center;
            margin: 4rem 0;
        }
        .section-divider::before {
            content: '';
            position: absolute;
            top: 50%;
            left: 0;
            right: 0;
            height: 1px;
            background: linear-gradient(to right, transparent, #e5e7eb, transparent);
        }
        .section-divider span {
            background: white;
            padding: 0 2rem;
            position: relative;
            color: #9ca3af;
        }
        .timeline-item {
            position: relative;
            padding-left: 3rem;
            margin-bottom: 2rem;
        }
        .timeline-item::before {
            content: '';
            position: absolute;
            left: 0.75rem;
            top: 0;
            bottom: -2rem;
            width: 2px;
            background: #e5e7eb;
        }
        .timeline-item:last-child::before {
            bottom: 0;
        }
        .timeline-dot {
            position: absolute;
            left: 0;
            top: 0.5rem;
            width: 1.5rem;
            height: 1.5rem;
            background: white;
            border: 3px solid #667eea;
            border-radius: 50%;
        }
        .mermaid {
            display: flex;
            justify-content: center;
            margin: 2rem 0;
        }
    </style>
</head>
<body class="bg-gray-50">
    <!-- Hero Section -->
    <section class="hero-gradient text-white py-20 px-6">
        <div class="max-w-6xl mx-auto">
            <div class="floating-animation">
                <i class="fas fa-recycle text-6xl mb-6 opacity-80"></i>
            </div>
            <h1 class="text-5xl md:text-6xl font-bold mb-6 serif-font">
                深入理解 CMS 垃圾收集器中的<br>
                <span class="text-yellow-300">浮动垃圾</span>
            </h1>
            <p class="text-xl md:text-2xl mb-8 opacity-90 max-w-3xl">
                探索 Java 虚拟机中 CMS 垃圾收集器的并发机制，理解浮动垃圾的产生原理及其对系统性能的影响
            </p>
            <div class="flex flex-wrap gap-4 text-sm">
                <span class="bg-white bg-opacity-20 px-4 py-2 rounded-full">
                    <i class="fas fa-clock mr-2"></i>低停顿时间
                </span>
                <span class="bg-white bg-opacity-20 px-4 py-2 rounded-full">
                    <i class="fas fa-sync mr-2"></i>并发清理
                </span>
                <span class="bg-white bg-opacity-20 px-4 py-2 rounded-full">
                    <i class="fas fa-memory mr-2"></i>内存优化
                </span>
            </div>
        </div>
    </section>

    <!-- Main Content -->
    <main class="max-w-6xl mx-auto px-6 py-12">
        <!-- Introduction -->
        <section class="bg-white rounded-2xl shadow-lg p-8 md:p-12 mb-12 card-hover">
            <p class="text-lg leading-relaxed text-gray-700">
                <span class="drop-cap serif-font">在</span>Java 虚拟机 (JVM) 中，CMS (Concurrent Mark-Sweep) 是一种面向低停顿的垃圾收集器，其设计目的是在应用运行时尽量减少垃圾收集的停顿时间，以提升应用的响应性。然而，由于 CMS 的并发清理机制，会产生一种被称为"浮动垃圾"（Floating Garbage）的现象，这也是 CMS 垃圾收集器的一个特性和难题。
            </p>
        </section>

        <!-- CMS Working Mechanism -->
        <section class="mb-16">
            <div class="flex items-center mb-8">
                <i class="fas fa-cogs text-4xl text-purple-600 mr-4"></i>
                <h2 class="text-3xl font-bold gradient-text">CMS 垃圾收集器的工作机制</h2>
            </div>
            
            <div class="bg-white rounded-2xl shadow-lg p-8 mb-8">
                <p class="text-gray-700 mb-6">CMS 垃圾收集器采用的是"标记-清除"算法，执行流程主要分为以下几个阶段：</p>
                
                <div class="space-y-4">
                    <div class="timeline-item">
                        <div class="timeline-dot"></div>
                        <div class="bg-purple-50 rounded-lg p-6 card-hover">
                            <h3 class="text-xl font-semibold text-purple-800 mb-2">
                                <i class="fas fa-flag mr-2"></i>初始标记 (Initial Mark)
                            </h3>
                            <p class="text-gray-700">短暂停顿，标记 GC Roots 直接引用的对象。</p>
                        </div>
                    </div>
                    
                    <div class="timeline-item">
                        <div class="timeline-dot"></div>
                        <div class="bg-blue-50 rounded-lg p-6 card-hover">
                            <h3 class="text-xl font-semibold text-blue-800 mb-2">
                                <i class="fas fa-search mr-2"></i>并发标记 (Concurrent Mark)
                            </h3>
                            <p class="text-gray-700">在应用线程并发运行的情况下标记可达对象。</p>
                        </div>
                    </div>
                    
                    <div class="timeline-item">
                        <div class="timeline-dot"></div>
                        <div class="bg-indigo-50 rounded-lg p-6 card-hover">
                            <h3 class="text-xl font-semibold text-indigo-800 mb-2">
                                <i class="fas fa-redo mr-2"></i>重新标记 (Remark)
                            </h3>
                            <p class="text-gray-700">短暂停顿，标记在并发标记阶段后存活的对象，确保引用链的完整性。</p>
                        </div>
                    </div>
                    
                    <div class="timeline-item">
                        <div class="timeline-dot"></div>
                        <div class="bg-green-50 rounded-lg p-6 card-hover">
                            <h3 class="text-xl font-semibold text-green-800 mb-2">
                                <i class="fas fa-broom mr-2"></i>并发清除 (Concurrent Sweep)
                            </h3>
                            <p class="text-gray-700">与应用线程并发运行，清除标记阶段判定为不可达的对象。</p>
                        </div>
                    </div>
                </div>
                
                <div class="mt-8 p-6 bg-yellow-50 rounded-lg border-l-4 border-yellow-400">
                    <p class="text-gray-700">
                        <i class="fas fa-info-circle text-yellow-600 mr-2"></i>
                        其中，并发标记和并发清除阶段都是和应用程序线程并行运行的，这意味着在 CMS 垃圾收集器执行清除时，应用程序仍然可以创建新对象。这些新创建的对象可能在 CMS 的清除阶段被视为垃圾，但在应用线程继续运行的过程中，实际已经不再被引用了。这些对象即为"浮动垃圾"。
                    </p>
                </div>
            </div>
        </section>

        <!-- Floating Garbage Generation -->
        <section class="mb-16">
            <div class="flex items-center mb-8">
                <i class="fas fa-ghost text-4xl text-purple-600 mr-4"></i>
                <h2 class="text-3xl font-bold gradient-text">浮动垃圾的产生过程</h2>
            </div>
            
            <div class="bg-white rounded-2xl shadow-lg p-8">
                <p class="text-gray-700 mb-6">"浮动垃圾"是在 CMS 清理阶段由于垃圾收集与应用程序并发运行所产生的临时不可达对象。主要的形成机制如下：</p>
                
                <div class="grid md:grid-cols-3 gap-6 mb-8">
                    <div class="bg-gradient-to-br from-purple-100 to-pink-100 rounded-xl p-6 card-hover">
                        <div class="text-purple-600 text-3xl mb-4">
                            <i class="fas fa-plus-circle"></i>
                        </div>
                        <h4 class="font-semibold text-gray-800 mb-2">对象分配</h4>
                        <p class="text-gray-600 text-sm">在 CMS 的并发标记阶段之后到并发清除阶段期间，应用程序可能会分配大量新对象，这些对象会被 CMS 垃圾收集器记录为存活对象。</p>
                    </div>
                    
                    <div class="bg-gradient-to-br from-blue-100 to-indigo-100 rounded-xl p-6 card-hover">
                        <div class="text-blue-600 text-3xl mb-4">
                            <i class="fas fa-unlink"></i>
                        </div>
                        <h4 class="font-semibold text-gray-800 mb-2">引用丢失</h4>
                        <p class="text-gray-600 text-sm">如果这些新对象在此期间变为不可达（即被应用程序丢弃了所有引用），CMS 垃圾收集器并不会立即识别出这些对象的状态变更。</p>
                    </div>
                    
                    <div class="bg-gradient-to-br from-green-100 to-teal-100 rounded-xl p-6 card-hover">
                        <div class="text-green-600 text-3xl mb-4">
                            <i class="fas fa-hourglass-half"></i>
                        </div>
                        <h4 class="font-semibold text-gray-800 mb-2">延迟回收</h4>
                        <p class="text-gray-600 text-sm">当 CMS 进入并发清除阶段时，这些对象已经被认为是存活的，而不会被及时回收。这些"活着但不可达"的对象即是"浮动垃圾"。</p>
                    </div>
                </div>
                
                <!-- Mermaid Diagram -->
                <div class="bg-gray-50 rounded-lg p-6">
                    <h4 class="text-center text-gray-700 font-semibold mb-4">浮动垃圾产生流程图</h4>
                    <div class="mermaid">
                        graph LR
                            A[应用线程运行] --> B[创建新对象]
                            B --> C{CMS并发标记}
                            C -->|标记为存活| D[对象被记录]
                            D --> E[应用继续运行]
                            E --> F[对象失去引用]
                            F --> G{CMS并发清除}
                            G -->|已标记存活| H[不被清除]
                            H --> I[成为浮动垃圾]
                            I --> J[等待下次GC]
                    </div>
                </div>
                
                <div class="mt-6 p-4 bg-purple-50 rounded-lg">
                    <p class="text-gray-700 text-sm">
                        <i class="fas fa-lightbulb text-purple-600 mr-2"></i>
                        